package com.kuangstudy.controller;

import com.kuangstudy.anno.MethodCallCount;
import com.kuangstudy.aspect.MethodCallCountAspect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author panghu
 * @version 1.0.0
 * @createTime 2024年02月15日 13:38:00
 * @Description
 */
@RestController
@Api(tags = "统计bean的调用次数")
public class BeanCountController {

    @Autowired
    private MethodCallCountAspect callCountAspect;

    @GetMapping("/test")
    @MethodCallCount
    @ApiOperation("测试模拟调用bean方法")
    public String test() {
        return "success";
    }

    @GetMapping("/getCallMethodCount")
    @ApiOperation("获取某个方法的正常调用次数")
    public Integer getCallMethodCount(String methodName) {
        String className = BeanCountController.class.getName();
        return callCountAspect.getNormalMethodCallCountNumber(className + "." + methodName);
    }

    @GetMapping("/getCallMethodCountEx")
    @ApiOperation("获取某个方法的异常调用次数")
    public Integer getCallMethodCountEx(String methodName) {
        String className = BeanCountController.class.getName();
        return callCountAspect.getExMethodCallCountNumber(className + "." + methodName);
    }

    @GetMapping("/getCallMethodCountAny")
    @ApiOperation("获取某个方法的所有的调用次数")
    public Integer getCallMethodCountAny(String methodName) {
        String className = BeanCountController.class.getName();
        return callCountAspect.getAnyMethodCallCountNumber(className + "." + methodName);
    }

    @GetMapping("/methodEx")
    @ApiOperation("模拟出现异常")
    @MethodCallCount
    public Integer methodEx() {
        return 1 / 0;
    }

}
